package com.printeverywhere.job;

import static com.printeverywhere.util.LoggerConstants.USERS_COUNT_EXCEPTION;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.SystemMaintenance;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.HibernateUtil;

public class PrunePendingItemsJob implements Job {
	private static final Logger logger = Logger
			.getLogger(PrunePendingItemsJob.class);

	private SystemMaintenance systemMaintenance;

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("execute(JobExecutionContext) - start"); //$NON-NLS-1$
			}
			systemMaintenance = (SystemMaintenance) CommonFunctionDelegator.getAllfromtable(SystemMaintenance.class.getName()).get(AdminConstants.ZERO_NUMBER);
			int daysToSubtract = (systemMaintenance.getOldAuctions() * -1);
			Session session = HibernateUtil.getSession();
			Criteria criteria = session.createCriteria(Items.class);
			criteria.add(Expression.le("endTime", DateUtil.addDate(DateUtil.getTodaysDate(), Calendar.DATE, daysToSubtract)));
			Transaction transaction = session.beginTransaction();
			List<Items> list;
			try {
				list = criteria.list();
				transaction.commit();
			} catch (HibernateException e) {
				logger.error(USERS_COUNT_EXCEPTION);
				throw e;
			} finally {
				if (logger.isInfoEnabled()) {
					logger.info(AttributeConstants.CLOSING_SESSION);
				}
				session.flush();
				session.close();
				HibernateUtil.closeSession();
			}

			if(list != null){
				for (Iterator<Items> iterator = list.iterator(); iterator.hasNext();) {
					Items items = (Items) iterator.next();
//					CommonFunctionDelegator.GeneralDelete(items);
				}
				System.out.println("===> " + list.size());
			} else{
				System.out.println("No record found...");
			}
			if (logger.isDebugEnabled()) {
				logger.debug("execute(JobExecutionContext) - end"); //$NON-NLS-1$
			}
		} catch (Exception e) {
	}

	}
	public static void main(String[] args) {
		SystemMaintenance systemMaintenance = (SystemMaintenance) CommonFunctionDelegator.getAllfromtable(SystemMaintenance.class.getName()).get(AdminConstants.ZERO_NUMBER);
		int daysToSubtract = (systemMaintenance.getOldAuctions() * -1);
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Items.class);
		criteria.add(Expression.le("endTime", DateUtil.addDate(DateUtil.getTodaysDate(), Calendar.DATE, daysToSubtract)));
		Transaction transaction = session.beginTransaction();
		List<Items> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(USERS_COUNT_EXCEPTION);
			throw e;
		}
		session.close();
		System.out.println(list.size());
	}
}
